#!/bin/sh

# Author: Zhang Huangbin (michaelbibby <at> gmail.com)

# chkconfig: 2345 82 28
# description: mailgraph postfix log grapher, based on official script of mailgraph.
#
# processname: mailgraph.pl
# pidfile: /var/run/mailgraph.pid


. /etc/rc.d/init.d/functions

PATH='/bin:/usr/bin'
MAILGRAPH_PL='/usr/local/bin/mailgraph.pl'
MAIL_LOG='/var/log/maillog'
PID_FILE='/var/run/mailgraph.pid'
RRD_DIR='/var/lib/mailgraph'

start()
{
    echo -n "Starting mailgraph:"
    nice -19 $MAILGRAPH_PL -l $MAIL_LOG -d --daemon-pid=$PID_FILE --daemon-rrd=$RRD_DIR && success || failure
    echo
}

stop()
{
    if [ -f $PID_FILE ]; then
        echo -n "Stopping mailgraph:"
        kill $(cat $PID_FILE) && rm $PID_FILE && success || failure
        echo
    else
        echo -n "mailgraph is not running." && success
        echo
    fi
}

status()
{
    echo -n "Check mailgraph status:"

    if [ -f ${PID_FILE} ]; then
        success && echo
    else
        failure || echo
    fi
}

usage()
{
    echo "Usage: $0 [start|stop|restart|status]"
}

case $1 in
    start) start ;;
    stop) stop ;;
    restart) stop && start ;;
    status) status ;;
    *) usage ;;
esac
